package com.cms.cmsbackend.controller;

import com.cms.cmsbackend.pojo.Category;
import com.cms.cmsbackend.service.CategoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.GeneratedValue;
import java.util.concurrent.Callable;

@RestController
@Api(tags = "栏目管理")
@RequestMapping("/Category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping("/add")
    @ApiOperation("栏目新增")
    public String add(Category category){
        categoryService.save(category);
        return "新增成功";
    }

    @GetMapping("/selectByPage")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "size",value = "页面大小",required = true)
    })
    public PageInfo selectByPage(int page, int size){
        PageInfo pageInfo=categoryService.findByPage(page,size);
        return pageInfo;
    }

    @GetMapping("/delete")
    @ApiOperation("删除栏目")
    @ApiImplicitParam(name="id",value = "栏目id",required = true)
    public String delete(int id){
        categoryService.remove(id);
        return "删除成功";
    }

    @GetMapping("/batchDelete")
    @ApiOperation("批量删除")
    @ApiImplicitParam(name="ids",value = "批量栏目id,写法如下：1,2,3,4",required = true)
    public String batchdel(int[] ids){
        categoryService.batchrem(ids);
        return "批量删除成功";
    }

    @GetMapping("/select")
    @ApiOperation("栏目查询")
    @ApiImplicitParam(name = "id",value = "查询栏目id",required = true)
    public Category select(int id){
        Category category=categoryService.choose(id);
        return category;
    }

    @PostMapping("/uodata")
    @ApiOperation("栏目更新")
    public String updata(Category category){
        categoryService.upadta(category);
        return "更新成功";
    }
}
